<?php

namespace App\Service\Index;

use App\Model\CoinLog;
use App\Model\UserFollow;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\BaseService;
use App\Service\ServiceTrait\GetUserInfoTrait;

class CallDetailsService extends BaseService
{
    use GetUserInfoTrait;
    public function handle()
    {
        $userId = $this->getUserId();
//        $userId = 19;

        $channe_id = $this->request->input('channe_id', '');

        $callType = $this->request->input('type',1);    // 1 = video  0= voice

        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;

        $logInfo = $callCls::getInfoByChannelId($channe_id);

        $chatUserId = $logInfo->user_id == $userId ? $logInfo->chat_user_id : $logInfo->user_id;
        $coinInfo = CoinLog::getDataByOrderId($logInfo->order_id,$userId);

        $coin = 0;
        foreach ($coinInfo as $one)
        {
            $coin += $one['coin'];
        }

//        $userInfo = $this->getUserInfo($userId);
        $chatUserInfo = $this->getUserInfo($chatUserId);
        return [
            'avatar' => $chatUserInfo->avatar,
            'nick_name' => $chatUserInfo->nick_name,
            'chat_user_id' => $chatUserId,
            'call_time' => gmdate("i:s", $logInfo->call_time),
            'earnings' =>$coin,
            //是否关注
            'followed' => UserFollow::check($userId, $chatUserId),
        ];
    }
}